<template>
  <div style="width: 100%">
    <div id="deawer">
      <v-tableTitle :btnConfig="btnConfig" @cancel="cancel" @save="save"></v-tableTitle>
      <div class="detailBox">
        <v-detail ref="detail"></v-detail>
      </div>
      <div class="detailListBox">
        <v-detailList ref="detailList"></v-detailList>
      </div>
    </div>
  </div>

</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
import skipMixin from "@/mixins/SkipMixin";

import tableTitle from "commons/TableTitle";
import detail from "../details/Detail"
import detailList from "../detailsList/DetailList"
import table from "commons/Table";
import config from "../Config";

export default {
  name: "",
  mixins: [baseMixin, skipMixin, config],
  data () {
    return {
      flag: false,
      // btnConfig: [
      //   { label: this.$t('common.save'), certigier: this.saveStatus||!this.btnPermNameList.includes('WM010409'), invalid: "save" },
      //   { label: this.$t('common.return'), certigier: !this.btnPermNameList.includes('WM010410'),  invalid: "cancel" },
      // ],
    };
  },
  computed: {
    btnConfig() {
      return [
        { label: this.$t('common.save'), certigier: this.saveStatus||!this.btnPermNameList.includes('WM010409'), invalid: "save" },
        { label: this.$t('common.return'), certigier: !this.btnPermNameList.includes('WM010410'),  invalid: "cancel" },
      ]
    }
  },
  created () {
  },
  methods: {
    //保存按钮
    save () {
      let that = this;
      that.$refs.detail.$children[1].$refs.input.$refs["from"].validate(async (valid) => {
        if (valid) {
          let detail = that.$refs.detail.detailData;
          if (that.flag) {
            that.$refs.detailList.$children[2].$refs.input.$refs["from"].validate(async (flag) => {
              if (flag) {
                let detailList = that.$refs.detailList.detailData;
                detailList.storerkey = detail.storerkey
                if (detailList.qtyordered == 0) {
                  that.$message.error(this.$t('msg.msg52'));
                  return;
                }
                let data = detail
                data.podetail = [];
                data.podetail.push(detailList);
                try {
                  let res = await that.$api.post('/savePo', data);
                  

                    that.cancel();

                    that.$message.success(res.returnMgr);

                } catch (e) {
                  that.$message.error(e)
                }
              }
            })

          } else {
            if (that.$store.state.temporary) {
              that.$refs.detailList.$children[2].$refs.input.$refs["from"].validate(async (flag) => {
                if (flag) {
                  let detailList = that.$refs.detailList.detailData;
                  detailList.storerkey = detail.storerkey
                  if (detailList.qtyordered == 0) {
                    that.$message.error(this.$t('msg.msg52'));
                    return;
                  }
                  let data = detail
                  data.podetail = [];
                  data.podetail.push(detailList);
                  try {
                    let res = await that.$api.post('/savePo', data);
                    
                      that.$refs.detailList.detailNav = "1";
                      that.$store.commit("setTemporary", "");
                      that.$message.success(res.returnMgr);

                  } catch (e) {
                    that.$message.error(e)
                  }
                }
              })
            } else {
              let data = detail
              try {
                let res = await that.$api.post('/savePo', data);
                
                  that.$message.success(res.returnMgr);
              } catch (e) {
                that.$message.error(e)
              }
            }
          }

        }
      })

    },
    //弹框取消
    cancel () {
      this.$router.push({ name: "po" });
    },
  },
  mounted () {
    if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip;
      this.flag = data.newFlag;
    } else if (this.$store.state.newFlag) {
      this.flag = true;
    }
  },
  watch: {},
  components: {
    "v-tableTitle": tableTitle,
    "v-detail": detail,
    "v-detailList": detailList,
    "v-table": table
  }
}
</script>

<style scoped>
</style>
